/**
 *  created by lllwx
 *  Date: 2021/6/1
 *  Time: 2:41 下午
 *  Version: 1.0
 *  For: 分页
 */
import { ref, watch, unref } from '@vue/composition-api';

export function usePage(queryList, pageSize = 10) {
  const basePage = 1;

  const pageLimit = ref({
    pageSize: basePage,
    pageNo: 1,
  });

  pageLimit.value.pageSize = pageSize;

  watch(() => pageLimit.value.pageNo, () => {
    queryList();
  });

  watch(() => pageLimit.value.pageSize, () => {
    if (pageLimit.value.pageNo !== 1) {
      pageLimit.value.pageNo = 1;
    } else {
      queryList();
    }
  });

  // 重置分页条件
  function resetPageLimit() {
    if (unref(pageLimit).pageNo === basePage && unref(pageLimit).pageSize === pageSize) {
      queryList();
    } else {
      pageLimit.value.pageNo = basePage;
      pageLimit.value.pageSize = pageSize;
    }
  }

  return {
    pageLimit,
    resetPageLimit,
  };
}
